package com.tanxi521.tnweb.controller;

import com.tanxi521.tnweb.entity.Player;
import com.tanxi521.tnweb.entity.ResultVo;
import com.tanxi521.tnweb.service.PlayerService;
import com.tanxi521.tnweb.utils.PlayerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class BasicController {

    @Autowired
    PlayerService playerService;

    @RequestMapping("/toIndex")
    public String toIndex() {
        return "home";
    }

    @RequestMapping("sys/login")
    @ResponseBody
    public ResultVo login(HttpSession session , String playerName, String pass) {
        ResultVo resultVo = new ResultVo();
        try {
            Player player = playerService.login(playerName, pass);
            resultVo.setLogin(true);
            resultVo.setMessage("登录成功");
            session.setAttribute("player" , player);
        } catch (PlayerException e) {
            resultVo.setLogin(false);
            resultVo.setMessage(e.getMessage());
        }
        return resultVo;
    }
}
